﻿Imports DBGlue
Imports System.Web
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions

Class MainWindow

    Private _DB As DBContext

    Private Sub OnImportStartClick(sender As Object, e As RoutedEventArgs)
        _DB = New DBContext("c:\temp\watchdb.db3")

        Using fs As New StreamReader("Ref1.htm")
            Dim Data As String = fs.ReadToEnd
            ParseData(Data)
            fs.Close()
        End Using

    End Sub

    Private Function RequestPage(ByVal URL As String, Optional UseAuth As Boolean = True) As String

        Dim Http As HttpWebRequest = CType(HttpWebRequest.Create(URL), HttpWebRequest)

        Dim wr As WebResponse
        'Dim Encoder As New System.Text.ASCIIEncoding
        Dim rs As System.IO.StreamReader
        Dim Response As String
        wr = Http.GetResponse()
        rs = New System.IO.StreamReader(wr.GetResponseStream())
        Response = rs.ReadToEnd

        Return Response

    End Function

    Private Sub ParseData(Data As String)

        Dim reg As New Regex("Seiko <balise id="".*?"">([^<]*?)</?balise>([^<]*?)</font>.*?<img src=""(.*?)"".*?<div style=""margin:auto;text-align:center;width:100%"">((.*?)<br />)*?<a")
        Dim SeikoRef As Byte() = _DB.GetBrandID("Seiko")
        Dim counter As Integer = 0
        For Each m As Match In reg.Matches(Data)
            Dim ref As String = m.Groups(1).Value
            Dim Name As String = m.Groups(2).Value
            Dim Img As String = m.Groups(3).Value

            _DB.UpdateorInSertWatch(ref, Name, SeikoRef)
            counter += 1
        Next

        MessageBox.Show("matched " & counter & "refs")
    End Sub

End Class
